package work.comeback.omo.controller.listener;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletRequestListener;
import jakarta.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

public class AppRouterListener implements ServletContextListener, HttpSessionListener, ServletRequestListener {
    private static final Logger logger = LoggerFactory.getLogger(AppRouterListener.class);

    // 上下文创建
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("AppRoute already setup!");
        ServletContext servletContext = sce.getServletContext();

        // 添加路径映射
        HashMap<String, String> routePathList = new HashMap<>();

        // resource
        routePathList.put("/movie", "/WEB-INF/views/home/movie_detail.jsp");
        routePathList.put("/admin", "/WEB-INF/views/admin/index.jsp");
        routePathList.put("/placeholder.svg", "/assets/img/movie-img.jpg");

        // auth
        routePathList.put("/login", "/WEB-INF/views/auth/index.jsp");
        routePathList.put("/register", "/WEB-INF/views/auth/index.jsp");

        // dashboard panel
        routePathList.put("/panel/defaultPanel", "/WEB-INF/views/admin/panel/defaultPanel.jsp");
        routePathList.put("/panel/userManager", "/WEB-INF/views/admin/panel/userManager.jsp");
        routePathList.put("/panel/movieManager", "/WEB-INF/views/admin/panel/movieManager.jsp");
        routePathList.put("/panel/carouselManager", "/WEB-INF/views/admin/panel/carouselManager.jsp");
        routePathList.put("/panel/taskManager", "/WEB-INF/views/admin/panel/taskManager.jsp");
        routePathList.put("/panel/settingManager", "/WEB-INF/views/admin/panel/settingManager.jsp");

        // write to applicationScope
        servletContext.setAttribute("routePathList", routePathList);
        servletContext.setAttribute("appTitle", "oMo 影院");
    }
}
